from django.db import models

# Create your models here.
from malonghui.utils.models import BaseModel


class NewsCategory(BaseModel):
    """新闻分类"""
    name = models.CharField(max_length=16, verbose_name='分类名称')
    sequence = models.IntegerField(default=0, verbose_name='排序')

    class Meta:
        db_table = 'tb_news_category'
        verbose_name = '新闻分类'
        verbose_name_plural = verbose_name


class News(BaseModel):
    """头条新闻"""
    NEWS_SOURCE_ENUM = {
        "original": 1,
        "transmit": 2,
    }

    NEWS_SOURCE_CHOICES = (
        (1, '原创'),
        (2, '转载')
    )
    title = models.CharField(max_length=16, verbose_name='标题')
    content = models.TextField(verbose_name='内容')
    source = models.SmallIntegerField(choices=NEWS_SOURCE_CHOICES, default=1, verbose_name='新闻来源')
    digest = models.CharField(max_length=128, verbose_name='新闻摘要')
    clicks = models.IntegerField(verbose_name='点击量', default=0)
    status = models.BooleanField(verbose_name='是否审核通过', default=False)
    reason = models.CharField(max_length=128, verbose_name='被拒原因', null=True)
    collect = models.IntegerField(verbose_name='收藏数')
    user_id = models.ForeignKey('users.User', on_delete=models.CASCADE, verbose_name='作者用户')
    category_id = models.ForeignKey(NewsCategory, verbose_name='新闻分类')

    class Meta:
        db_table = 'tb_news'
        verbose_name = '头条新闻'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class NewsComment(BaseModel):
    """新闻评论"""
    content = models.CharField(max_length=256, verbose_name="评论内容")
    like_count = models.IntegerField(default=0, verbose_name="点赞条数")
    news_id = models.ForeignKey(News, verbose_name="所属新闻")
    user_id = models.ForeignKey('users.User', verbose_name="评论用户")
    parent = models.ForeignKey('self', null=True, blank=True, related_name='rela_comments', verbose_name="父级新闻")

    class Meta:
        db_table = 'tb_news_comment'
        verbose_name = '新闻评论'
        verbose_name_plural = verbose_name


class NewsCollect(BaseModel):
    """新闻收藏"""
    news_id = models.ForeignKey(News, verbose_name='新闻')
    user_id = models.ForeignKey('users.User', verbose_name='用户')

    class Meta:
        db_table = 'tb_news_collect'
        verbose_name = '新闻收藏'
        verbose_name_plural = verbose_name


class Content(BaseModel):
    """广告"""
    name = models.CharField(max_length=32, verbose_name="广告名")
    image_url = models.CharField(max_length=2048, verbose_name="图片链接")
    sequence = models.IntegerField(default=1, verbose_name="排序")
    status = models.BooleanField(default=1, verbose_name="是否展示")

    class Meta:
        db_table = 'tb_content'
        verbose_name = '广告'
        verbose_name_plural = verbose_name


class Classes(BaseModel):
    """首页版块"""
    name = models.CharField(max_length=8, verbose_name="版块名称")
    sequence = models.IntegerField(default=1, verbose_name="排序")

    class Meta:
        db_table = 'tb_classes'
        verbose_name = '首页版块'
        verbose_name_plural = verbose_name
